package com.kbt.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.android.volley.Request;
import com.kbt.activity.*;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.listener.ClickFragmentStartActForResultListener;
import com.kbt.listener.ClickFragmentStartActForResultListenerCopy;
import com.kbt.model.data.BaseBean;
import com.kbt.model.data.NewsListBean;
import com.kbt.model.data.RenZhengBean;
import com.kbt.model.data.RenZhengBeans;
import com.kbt.net.RequestManager;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.utils.SharePreferenceUtils;
import com.kbt.util.utils.Utils;
import org.apache.http.protocol.HTTP;

import java.util.HashMap;
import java.util.Map;

/**
 * 主页
 * Created by kuajie on 15/7/31.
 */
public class MyFragment extends Fragment implements RequestManager.ResponseInterface{
    private String userName ;
    private TextView userNameView;
    private ImageView userImg;
    private SharePreferenceUtils share;
    private TextView isShowBangDing;//是否显示已绑定
    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private BaseBean baseBean;
    private RenZhengBean renZhengBean;
    private RenZhengBeans renZhengBeans;

    /********
     * 接收返回数据
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == StaticData.MY_USER_FOR_LOGIN && resultCode == Activity.RESULT_OK && data != null) {
//            userName = data.getStringExtra("userName");
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_info, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        share = new SharePreferenceUtils(getActivity());
        userName = share.getString("user_mobile","");
        //初始化登录信息
        initLoginInfo();
        //初始化按钮信息
        initView();

        if(!"".equals(userName)&&userName!=null) {
            updateLoginInfo(userName);
            httpisCheckBangding(userName);
        }else{
            isShowBangDing.setVisibility(View.INVISIBLE);
        }
    }
    //判断账号是否绑定
    private void httpisCheckBangding(String phone){
        if(Utils.isNetworkAvailable(getActivity())) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.isCheckBangDingUrl+phone, Constants.RequestTag.isCheckBangDingTag, map, RenZhengBeans.class);
        }else{
            Toast.makeText(getActivity(), R.string.Wangluo, Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();
        isShowBangDing = (TextView) getActivity().findViewById(R.id.isShowBangDing);
        //订单Activity
        Map<String, Object> params = new HashMap<String, Object>();
        //全部订单
        Button allOrderBtn = (Button) getActivity().findViewById(R.id.order_all_btn);
        Button allOrderArrow = (Button) getActivity().findViewById(R.id.orderallArrow);
        params.put(StaticData.ORDER_TYPE, StaticData.ALL_ORDER);
        allOrderBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, OrderActivity.class,
                StaticData.MY_USER_FOR_LOGIN, params));
        allOrderArrow.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, OrderActivity.class,
                StaticData.MY_USER_FOR_LOGIN, params));

        //待付款订单
        Button waitPayBtn = (Button) getActivity().findViewById(R.id.waitPayOrderBtn);
        params = new HashMap<String, Object>();
        params.put(StaticData.ORDER_TYPE, StaticData.WAIT_PAY_ORDER);
        waitPayBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, OrderActivity.class,
                StaticData.MY_USER_FOR_LOGIN, params));
        //待发货订单
        Button waitSendBtn = (Button) getActivity().findViewById(R.id.waitSendOrderBtn);
        params = new HashMap<String, Object>();
        params.put(StaticData.ORDER_TYPE, StaticData.WAIT_SEND_ORDER);
        waitSendBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, OrderActivity.class,
                StaticData.MY_USER_FOR_LOGIN, params));
        //待收货订单
        Button hasSendBtn = (Button) getActivity().findViewById(R.id.hasSendOrderBtn);
        params = new HashMap<String, Object>();
        params.put(StaticData.ORDER_TYPE, StaticData.WAIT_RECIVE_ORDER);
        hasSendBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, OrderActivity.class,
                StaticData.MY_USER_FOR_LOGIN, params));
        //完成订单
        Button finishBtn = (Button) getActivity().findViewById(R.id.finshOrderBtn);
        params = new HashMap<String, Object>();
        params.put(StaticData.ORDER_TYPE, StaticData.FINISH_ORDER);
        finishBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, OrderActivity.class,
                StaticData.MY_USER_FOR_LOGIN, params));

        //实名认证

        LinearLayout rzBtn = (LinearLayout) getActivity().findViewById(R.id.renZhengBtn);
        Button RZBtn = (Button) getActivity().findViewById(R.id.RZBtn);
        Button RZArrow = (Button) getActivity().findViewById(R.id.RZArrow);


                rzBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(BaseApplication.getInstance().isLogin()) {
                            if (renZhengBeans.getResult().equals("success")) {
                                Intent intent = new Intent(getActivity(), OverRenZhengActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("renzhengBean", renZhengBean);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(getActivity(), RenZhengActivity.class);
                                startActivity(intent);
                            }
                        }else{
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                        }
                    }
                });
                RZBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(BaseApplication.getInstance().isLogin()) {
                            if (renZhengBeans.getResult().equals("success")) {
                                Intent intent = new Intent(getActivity(), OverRenZhengActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("renzhengBean", renZhengBean);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(getActivity(), RenZhengActivity.class);
                                startActivity(intent);
                            }
                        }else{
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                        }
                    }
                });
                RZBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(BaseApplication.getInstance().isLogin()) {
                            if (renZhengBeans.getResult().equals("success")) {
                                Intent intent = new Intent(getActivity(), OverRenZhengActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("renzhengBean", renZhengBean);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(getActivity(), RenZhengActivity.class);
                                startActivity(intent);
                            }
                        }else{
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                        }
                    }
                });



        //我的积分
        Button jfBtn = (Button) getActivity().findViewById(R.id.JFBtn);
        jfBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, MyInfoJFActivity.class,
                StaticData.MY_FOR_JF, null));
        Button jfArrow = (Button) getActivity().findViewById(R.id.JFArrow);
        jfArrow.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, MyInfoJFActivity.class,
                StaticData.MY_FOR_JF, null));


        //收获地址管理
        Button addressBtn = (Button) getActivity().findViewById(R.id.addressButton);
        addressBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, AddressMangeActivity.class,
                StaticData.MY_FOR_ADDRESS_MANGER, null));
        Button addressArrow = (Button) getActivity().findViewById(R.id.addressArrow);
        addressArrow.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, AddressMangeActivity.class,
                StaticData.MY_FOR_ADDRESS_MANGER, null));

        //邀请小伙伴
        Button shareBtn = (Button) getActivity().findViewById(R.id.shareBtn);
        shareBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, ShareToFriendActivity.class,
                StaticData.MY_FOR_ADDRESS_MANGER, null));
        Button shareArrow = (Button) getActivity().findViewById(R.id.shareArrow);
        shareArrow.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, ShareToFriendActivity.class,
                StaticData.MY_FOR_ADDRESS_MANGER, null));

        //帮助与反馈
        ImageView helpImg = (ImageView) getActivity().findViewById(R.id.helpImg);
        helpImg.setOnClickListener(new ClickFragmentStartActForResultListenerCopy(MyFragment.this, HelpAndFeedBackActivity.class,
                StaticData.MY_FOR_ADDRESS_MANGER, null));
        Button helpBtn = (Button) getActivity().findViewById(R.id.helpBtn);
        helpBtn.setOnClickListener(new ClickFragmentStartActForResultListenerCopy(MyFragment.this, HelpAndFeedBackActivity.class,
                StaticData.MY_FOR_ADDRESS_MANGER, null));
        Button helpArrow = (Button) getActivity().findViewById(R.id.helpArrow);
        helpArrow.setOnClickListener(new ClickFragmentStartActForResultListenerCopy(MyFragment.this, HelpAndFeedBackActivity.class,
                StaticData.MY_FOR_ADDRESS_MANGER, null));

        //客服信息

        ImageView callPhoneImg = (ImageView) getActivity().findViewById(R.id.callPhoneImg);
        // ImageView callPhoneImg = (ImageView) getActivity().findViewById(R.id.callPhoneBtn);
        TextView callPhoneText = (TextView) getActivity().findViewById(R.id.callPhoneText);
        TextView callPhoneText2 = (TextView) getActivity().findViewById(R.id.callPhoneTextTwo);
        callPhoneImg.setOnClickListener(new CallPhoneOnClickListener());
        callPhoneText.setOnClickListener(new CallPhoneOnClickListener());
        callPhoneText2.setOnClickListener(new CallPhoneOnClickListener());


    }

    /********
     * @title: 初始化头部登录信息
     * @description: 如用户未登录, 展示无用户登录的Layout
     * 否则:展示用户头像与名称
     */
    private void initLoginInfo() {
        LinearLayout noUserLayout = (LinearLayout) getActivity().findViewById(R.id.noLoginUser);
        LinearLayout loginUserLayout = (LinearLayout) getActivity().findViewById(R.id.HasLoginUser);

        //用户已登录
        if (!"".equals(userName)) {
            noUserLayout.setVisibility(View.GONE);
            loginUserLayout.setVisibility(View.VISIBLE);
        } else {
            noUserLayout.setVisibility(View.VISIBLE);
            loginUserLayout.setVisibility(View.GONE);
        }


        /***
         * 登录按钮
         */
        Button loginBtn = (Button) getActivity().findViewById(R.id.my_login_btn);
//        BtnSetting.setBtnBackground(loginBtn, 2, Color.RED, Color.TRANSPARENT, 5);
        loginBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, LoginActivity.class,
                StaticData.MY_USER_FOR_LOGIN, null));


        /****
         * 注册按钮事件
         */
        Button regBtn = (Button) getActivity().findViewById(R.id.my_register_btn);
//        BtnSetting.setBtnBackground(regBtn, 2, Color.RED, Color.TRANSPARENT, 5);
//        regBtn.setOnClickListener(new ClickFragmentStartActForResultListener(MyFragment.this, LoginRegisterActivity.class,
//                StaticData.MY_USER_FOR_LOGIN, null));
        regBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),LoginRegisterActivity.class);
                getActivity().startActivity(intent);
            }
        });

        userNameView = (TextView) getActivity().findViewById(R.id.userName);
    }

    /*****
     * 更新登录事件
     *****/
    private void updateLoginInfo(String loginUserName) {
        LinearLayout noUserLayout = (LinearLayout) getActivity().findViewById(R.id.noLoginUser);
        LinearLayout loginUserLayout = (LinearLayout) getActivity().findViewById(R.id.HasLoginUser);
        noUserLayout.setVisibility(View.GONE);
        loginUserLayout.setVisibility(View.VISIBLE);
        userNameView.setText(loginUserName+"");
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.isCheckBangDingTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                renZhengBeans = (RenZhengBeans) parsedGSON;
                if (renZhengBeans.getResult().equals("success")) {
                    renZhengBean = renZhengBeans.getData();
                    isShowBangDing.setVisibility(View.VISIBLE);
                }else{
                    isShowBangDing.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(getActivity(),R.string.Wangluo,Toast.LENGTH_SHORT).show();
    }

    /**
     * 调用拨打电话的事件
     */
    private class CallPhoneOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + StaticData.KF_CALL_PHONE));
                startActivity(intent);

        }
    }
}
